<?php
namespace Duobao\Model;

use Think\Log;
use Think\Model;


class OrderModel extends Model{
    protected $tableName = 'member_addmoney_record';

    /**
     * 订单的生成
     */
    public function CreateOrder($uid,$type,$data){
        $model = new \Think\Model();
        $totalMoney  = '';#根据前端传递的数据计算出来的金额

        #检测商品的数量能否生成订单，如果不能生成订单，直接返回错误页面，直接让前端跳转到失败页面
        foreach ($data as $item) {
            $totalMoney += $item['number'];
            $condition['id'] = $item['id'];
            $count = $model->table("__SHOPLIST__")->where($condition)->getField('shenyurenshu');
            if($count < $item['number']){
                #定义提示数组
                $ret['status'] = false;
                $ret['msg'] = '商品的剩余数量不足，下单失败';
                return $ret;#判断如果用户的购买的数量大于剩余数量，返回false
            }
        }

        //如果检测到订单的金额为0，直接返回
        if($totalMoney == 0) return false;
        $cOrder = productionOrder("C");

        $ins['code'] = $cOrder;
        $ins['uid'] = $uid;
        $ins['money'] = $totalMoney;
        $ins['time'] = time();

        if($type == 'balances'){
            $ins['pay_type'] = "帐户余额支付";
        }else{
            $ins['pay_type'] = "微信支付";
        }

        $ins['status'] = '未付款';
        $ins['scookies'] = serialize($data);


        $result = $model->table("__MEMBER_ADDMONEY_RECORD__")->add($ins);

        if($result){
            return $cOrder;
        }else{
            return false;
        }
    }
    public function CreateOrder2($uid,$type,$data){
        $model = new \Think\Model();
        $totalMoney  = '';#根据前端传递的数据计算出来的金额

        #检测商品的数量能否生成订单，如果不能生成订单，直接返回错误页面，直接让前端跳转到失败页面
        foreach ($data as $item) {
            $totalMoney += $item['number'];
            $condition['id'] = $item['id'];
            $count = $model->table("__SHOPLIST__")->where($condition)->getField('shenyurenshu');
            if($count < $item['number']){
                #定义提示数组
                $ret['status'] = false;
                $ret['msg'] = '商品的剩余数量不足，下单失败';
                return $ret;#判断如果用户的购买的数量大于剩余数量，返回false
            }
        }

        //如果检测到订单的金额为0，直接返回
        if($totalMoney == 0) return false;
        $cOrder = productionOrder("C");

        $ins['code'] = $cOrder;
        $ins['uid'] = $uid;
        $ins['money'] = $totalMoney;
        $ins['time'] = time();

        if($type == 'balances'){
            $ins['pay_type'] = "帐户余额支付";
        }else{
            $ins['pay_type'] = "在线支付";
        }

        $ins['status'] = '未付款';
        $ins['scookies'] = serialize($data);


        $result = $model->table("__MEMBER_ADDMONEY_RECORD__")->add($ins);

        if($result){
            return $cOrder;
        }else{
            return false;
        }
    }

    /**
     * @param $uid  用户id
     * @param $type 订单类型：微信充值
     * @param $data 充值金额
     *
     * @return string 订单串
     */
    public function createRechargeOrder($uid,$money){


        //如果检测到订单的金额为0，直接返回
        if($money < 0) return false;
        $cOrder = productionOrder("C");

        $ins['code'] = $cOrder;
        $ins['uid'] = $uid;
        $ins['money'] = $money;
        $ins['time'] = time();
        $ins['pay_type'] = "微信充值";
        $ins['status'] = '未付款';
        $ins['scookies'] = 0;
        $model = new \Think\Model();
        $result = $model->table("__MEMBER_ADDMONEY_RECORD__")->add($ins);

        if($result){
            return $cOrder;
        }else{
            return false;
        }
    }

    public function createRechargeOrder2($uid,$money){


        //如果检测到订单的金额为0，直接返回
        if($money < 0) return false;
        $cOrder = productionOrder("C");

        $ins['code'] = $cOrder;
        $ins['uid'] = $uid;
        $ins['money'] = $money;
        $ins['time'] = time();
        $ins['pay_type'] = "在线充值";
        $ins['status'] = '未付款';
        $ins['scookies'] = 0;
        $model = new \Think\Model();
        $result = $model->table("__MEMBER_ADDMONEY_RECORD__")->add($ins);

        if($result){
            return $cOrder;
        }else{
            return false;
        }
    }

    /*end order model*/
}